<template>
  <div class="container">
    <el-card>
      <el-form class="search" ref="form" :model="form" label-width="60px">
        <el-form-item label="关键字">
          <el-input
            v-model="form.keyword"
            placeholder="根据编号搜索"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="clear">清除</el-button>
          <el-button size="mini" type="primary" @click="serchQues"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      <el-alert type="info" class="alert" :closable="false" show-icon>
        数据一共 {{ this.counts }} 条
      </el-alert>
      <!-- 题组列表内容  -->
      <div class="table">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="编号" width=""> </el-table-column>
          <el-table-column prop="questionType" label="题型" width="">
            <template slot-scope="{ row }">
              {{ question(row.questionType) }}
            </template>
          </el-table-column>
          <el-table-column label="题目编号">
            <template slot-scope="{ row }">
              <el-link
                v-for="(item, index) in row.questionIDs"
                :key="index"
                type="primary"
                @click="previewQues(item)"
                >{{ item.number }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间"> </el-table-column>
          <el-table-column prop="totalSeconds" label="答题时间(s)">
          </el-table-column>
          <el-table-column prop="accuracyRate" label="正确率(%)">
          </el-table-column>
          <el-table-column prop="userName" label="录入人"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="deleteQues(row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pageTaion">
        <PageTool
          :paginationPage="requestParameters.page"
          :paginationPagesize="requestParameters.pagesize"
          :total="counts"
          @pageChange="handleCurrentChange"
          @pageSizeChange="handleSizeChange"
        />
      </div>
    </el-card>
    <el-dialog :visible.sync="showDialog" title="题目预览">
      <questionsDia ref="questionsDia" v-if="showDialog" />
    </el-dialog>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
import { questionType } from '@/api/hmmm/constants.js'

import PageTool from '@/module-dashboard/components/pageTool.vue'
import questionsDia from './questionsDia.vue'

export default {
  components: {
    PageTool,
    questionsDia
  },
  data () {
    return {
      form: {
        keyword: ''
      },
      tableData: [],
      counts: 0,
      requestParameters: {
        page: 1,
        pagesize: 10
        // username: this.username
      },
      showDialog: false
    }
  },
  created () {
    this.loaderRandom()
  },
  methods: {
    async loaderRandom () {
      const res = await randoms(this.requestParameters)
      console.log(res, 9999)
      if (res.status === 200) {
        this.tableData = res.data.items
        this.counts = res.data.counts
      }
    },
    question (num) {
      const obj = questionType.find((item) => item.value === Number(num))
      // console.log(obj.label)
      return obj.label
    },
    handleSizeChange (val) {
      this.requestParameters.pagesize = val

      this.loaderRandom(this.requestParameters)
    },
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.form.keyword = ''

      this.loaderRandom(this.requestParameters)
    },
    deleteQues (row) {
      this.$confirm('是否确认删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          if (this.tableData.length === 1 && this.requestParameters.page > 1) {
            this.requestParameters.page--
          }
          const res = await removeRandoms(row)
          console.log(res)
          if (res.status === 200) {
            this.loaderRandom()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    clear () {
      this.form = {
        keyword: ''
      }
    },
    serchQues () {
      // console.log(this.form.keyword)
      const reg = new RegExp(this.form.keyword)
      if (this.form.keyword) {
        const obj = this.tableData.filter((item) => reg.exec(item.id))
        // console.log(obj, 565665)
        this.tableData = obj
      } else {
        this.loaderRandom()
      }
    },
    async previewQues (row) {
      console.log(row, 10000)
      const obj = { id: '' }
      //  obj[row[id]]=row[]
      console.log(row.id)
      obj.id = row.id
      const res = await detail(obj)
      this.showDialog = true
      console.log(res, 7777)
      this.quesData = res.data

      this.$nextTick(() => {
        this.$refs.questionsDia.quesData = res.data
      })
    }
  }
}
</script>

<style scoped>
.container {
  padding: 10px 20px;
}
.search {
  display: flex;
  justify-content: space-between;
}
.alert {
  margin: 10px 0px;
  margin-top: 0px;
}
.el-alert__icon.is-big {
  font-size: 20px;
  width: 28px;
}
.el-button--danger {
  color: #f68e8c;
  background-color: #fef0f0;
  border-color: #fbcfcf;
}
.pageTaion {
  margin-top: 10px;
  text-align: right;
}
</style>
